package com.eat.fish_service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eat.common_utils.IpAndPortUtil;
import com.eat.common_utils.R;
import com.eat.fish_service.entity.Dao.Expert;
import com.eat.fish_service.entity.Dao.FishEncyclopedia;
import com.eat.fish_service.entity.Dao.User;
import com.eat.fish_service.entity.ExpertQuery;
import com.eat.fish_service.mapper.ExpertMapper;
import com.eat.fish_service.service.ExpertService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eat.fish_service.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author LWB
 * @since 2022-03-21
 */
@Service
public class ExpertServiceImpl extends ServiceImpl<ExpertMapper, Expert> implements ExpertService {

    @Value("${qualificationOne}")
    String qualificationOne;

    @Value("${qualificationTwo}")
    String qualificationTwo;

    @Autowired
    private ExpertService expertService;

    @Autowired
    private UserService userService;

    @Override
    public R addExpert(Expert expert) {
        User user = new User();
        user.setId(expert.getId());
        user.setRealName(expert.getRealName());
        user.setStatus(2);
        userService.updateById(user);

        int insert = baseMapper.insert(expert);
        if (insert == 1) {
            return R.ok();
        } else {
            return R.error();
        }

    }

    @Override
    public R addqualificationOnePic(MultipartFile pic) {

        if (pic != null) {
            String fileName = System.currentTimeMillis() + pic.getOriginalFilename();

            //第一次运行的时候，这个文件所在的目录往往是不存在的，这里需要创建一下目录
            File dir = new File(qualificationOne);
            if (!dir.exists()) {
                dir.mkdirs();
            }

            //把浏览器上传的文件复制到希望的位置
            File targetFile = new File(qualificationOne, fileName);
            try {
                pic.transferTo(targetFile);
            } catch (IOException e) {
                e.printStackTrace();
                return R.error().message("上传失败");
            }

            //服务器的ip和端口号
            String ipAndPort = IpAndPortUtil.getIpAndPort();
            String picUrl = "http://" + ipAndPort + "/getqualificationOne/" + fileName;

            //返回获取图片的url
            return R.ok().data("picUrl", picUrl);

        } else {
            return R.error().message("上传的文件为空！");
        }
    }

    @Override
    public R addqualificationTwoPic(MultipartFile pic) {

        if (pic != null) {
            String fileName = System.currentTimeMillis() + pic.getOriginalFilename();

            //第一次运行的时候，这个文件所在的目录往往是不存在的，这里需要创建一下目录
            File dir = new File(qualificationTwo);
            if (!dir.exists()) {
                dir.mkdirs();
            }

            //把浏览器上传的文件复制到希望的位置
            File targetFile = new File(qualificationTwo, fileName);
            try {
                pic.transferTo(targetFile);
            } catch (IOException e) {
                e.printStackTrace();
                return R.error().message("上传失败");
            }

            //服务器的ip和端口号
            String ipAndPort = IpAndPortUtil.getIpAndPort();
            String picUrl = "http://" + ipAndPort + "/getqualificationTwo/" + fileName;

            //返回获取图片的url
            return R.ok().data("picUrl", picUrl);

        } else {
            return R.error().message("上传的文件为空！");
        }
    }

    @Override
    public R pageExpertCondition(Long current, Long size, ExpertQuery expertQuery) {

        //创建page
        Page<Expert> pageFExpert = new Page<>(current, size);
        //构建条件
        QueryWrapper<Expert> wrapper = new QueryWrapper<>();
        //多条件查询
        String expertname = expertQuery.getRealName();
        String Qualification1 = expertQuery.getQualification1();
        String TimeBegin = expertQuery.getTimeBegin();
        String TimeEnd = expertQuery.getTimeEnd();
        String introduction = expertQuery.getIntroduce();

        wrapper.eq("is_deleted", 0);
        if (!StringUtils.isEmpty(expertname)) {
            wrapper.eq("real_name", expertname);
        }
        if (!StringUtils.isEmpty(Qualification1)) {
            wrapper.like("qualification1", Qualification1);
        }
        if (!StringUtils.isEmpty(introduction)) {
            wrapper.like("introduce", introduction);
        }
        if (!StringUtils.isEmpty(TimeBegin)) {
            wrapper.ge("gmt_create", TimeBegin);
        }
        if (!StringUtils.isEmpty(TimeEnd)) {
            wrapper.le("gmt_create", TimeEnd);
        }

        //调用方法实现查询
        expertService.page(pageFExpert, wrapper);

        long total = pageFExpert.getTotal();//获取总记录数
        List<Expert> records = pageFExpert.getRecords();//获取分页后的list集合
        long pages = pageFExpert.getPages();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("rows", records);
        if (current > pages) {
            return R.error().code(13).message("超过总页数");
        } else {
            return R.ok().data(map);
        }
    }

    @Override
    public R getExpert(String expertId) {

        Expert expert = expertService.getById(expertId);

        return R.ok().data("expert", expert);
    }

}
